<?php
declare (strict_types = 1);

namespace app\common\model\mysql;

use think\Model;

/**
 * @mixin \think\Model
 */
class User extends Model
{
    protected $autoWriteTimestamp = true;//自动生成时间
    /**
     * @param $phoneNumber 根据手机号查找数据
     * @return User|array|bool|null|Model
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getUserByPhoneNumber($phoneNumber)
    {
        if(empty($phoneNumber)){
            return false;
        }
        $where = [
            'phone_number'=>$phoneNumber,
        ];
        $result = $this->where($where)->find();
        return $result;
    }
    /**
     * 根据id获取数据
     * @param $id
     */
    public function getUserById($id){
        $id = intval($id);
        if(!$id){
            return false;
        }
        return $this->find($id);
    }

    /**
     * 根据用户名获取数据
     * @param $username
     * @return User|array|bool|null|Model
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getUserByUsername($username)
    {
        if(empty($username)){
            return false;
        }
        $where = [
            'phone_number'=>$username,
        ];
        $result = $this->where($where)->find();
        return $result;
    }
    /**
     * 根据主键id更新数据
     * @param $id
     * @param $data
     * @return bool
     */
    public function updateById($id,$data)
    {
        $id = intval($id);
        if(empty($id) || empty($data) || !is_array($data)){
            return false;
        }
        $where = [
            'id' => $id,
        ];
        return $this->where($where)->save($data);
    }
}
